<?php

class m_news_documents_ItemBase extends f_document_BaseDocument
{

	private $eventDates;
	private $pictures;
	private $header;
	private $content;
	private $url;
	private $websiteId;

	public function __construct($id = null, $lang = null)
	{
		parent::__construct($id, null, $lang);
	}

	public function getTableName()
	{
		return "m_news__item";
	}

	public function getDocumentModel()
	{
		return "modules_news/item";
	}
	
	protected function getFieldsBag()
	{
		return array('document_id', 			'document_uid', 			'document_model', 			'document_author', 			'document_creationdate', 			'document_modificationdate', 			'document_publicationstatus', 			'document_lang', 			'document_revision', 			'document_startpublicationdate', 			'document_endpublicationdate', 			'document_label', 			'eventDates', 			'pictures', 			'header', 			'content', 			'url', 			'websiteId');
	}

	public function getdefaultPublicationstatus()
	{
		return "DRAFT";
	}

	/**
	 * @return boolean
	 */
	public function isRevisable()
	{
		return false;
	}
	
	/**
	 * @return boolean
	 */
	public function isLocalized()
	{
		return true;
	}	
	
    /**
	 * @return boolean
	 */
	public function isRecursive()
	{
		return $this->isRecursive;
	}
	
	/**
	 * @param boolean $value
	 * @return m_news_documents_ItemBase
	 */	
	public function setIsRecursive($value)
	{
		$this->isRecursive = $value;
	}
		
	/**
	 * @return string
	 */
	public function getEventDates()
	{
		return $this->eventDates;
	}

	/**
	 * @param string $value
	 * @return m_news_documents_Item
	 */
	public function setEventDates($value)
	{
		$this->eventDates = $value;
		return $this;
	}
	/**
	 * @return integer
	 */
	public function getPictures()
	{
		return $this->pictures;
	}

	/**
	 * @param integer $value
	 * @return m_news_documents_Item
	 */
	public function setPictures($value)
	{
		$this->pictures = $value;
		return $this;
	}
	/**
	 * @return string
	 */
	public function getHeader()
	{
		return $this->header;
	}

	/**
	 * @param string $value
	 * @return m_news_documents_Item
	 */
	public function setHeader($value)
	{
		$this->header = $value;
		return $this;
	}
	/**
	 * @return string
	 */
	public function getContent()
	{
		return $this->content;
	}

	/**
	 * @param string $value
	 * @return m_news_documents_Item
	 */
	public function setContent($value)
	{
		$this->content = $value;
		return $this;
	}
	/**
	 * @return string
	 */
	public function getUrl()
	{
		return $this->url;
	}

	/**
	 * @param string $value
	 * @return m_news_documents_Item
	 */
	public function setUrl($value)
	{
		$this->url = $value;
		return $this;
	}
	/**
	 * @return integer
	 */
	public function getWebsiteId()
	{
		return $this->websiteId;
	}

	/**
	 * @param integer $value
	 * @return m_news_documents_Item
	 */
	public function setWebsiteId($value)
	{
		$this->websiteId = $value;
		return $this;
	}
	public function relationFields()
	{
		return array();
	}
	
	public function localizedFields()
	{
		return array(
                'document_id',
                'document_uid',
				'document_author', 
				'document_creationdate', 
				'document_modificationdate', 
				'document_lang', 
				'document_revision', 
				'document_label', 
				'document_publicationstatus', 'eventDates', 'pictures', 'content', 'url');
	}
	
	public function nonLocalizedFields()
	{
		return array(	'document_id', 			'document_uid', 			'document_model', 			'document_author', 			'document_creationdate', 			'document_modificationdate', 			'document_lang', 			'document_revision', 			'document_startpublicationdate', 			'document_endpublicationdate', 			'document_label', 			'header', 			'websiteId');
	}
	
	/**
	 * Returns VO lang and translations langs
	 * @return array
	 */
	public function getAllLangs()
	{
		$langs = $this->getTranslationLangs();
		array_unshift($langs, $this->getVO());
		return $langs;
	}
	
	/**
	 * Returns all translations langs 
	 * @return array
	 */
	public function getTranslationLangs()
	{
		$dbProvider = f_database_Provider::getInstance();
		
		$sql = "SELECT `document_lang` FROM ".$this->getTableName()."_i18n WHERE `document_uid`=:id";
		
		return $dbProvider->setQuery($sql)->execute(array("id" => $this->getUniqueId()))->getQueryResults(PDO::FETCH_COLUMN);
	}
	
	/**
	 * Check if current document has at least one translation or a translation with the given lang
	 * @return boolean
	 */
	public function hasTranslation($lang = null)
	{
		if(is_null($lang))
		{
			return count($this->getTranslationLangs()) > 0;
		}
		else
		{
			return in_array(strtoupper($lang), $this->getTranslationLangs());
		}
	}

	/**
	 * Check if current document is translated or not
	 * @return boolean
	 */
	public function isTranslated()
	{
		return $this->getPublicationstatus() != parent::NOTRANSLATION;
	}

	/**
	 * Check if current document is a translation
	 * @return boolean
	 */
	public function isATranslation()
	{
		return $this->getLang() != $this->getVO();
	}
	
	/**
	 * Return the original document
	 * @return m_news_documents_Item
	 */
	public function getVoDocument()
	{
		if($this->isATranslation())
		{
			return m_news_documents_Item::getInstanceByUniqueId($this->getUniqueId(), $this->getVO());
		}
		return $this;
	}
	
	/**
	 * Return current document translation
	 * @return m_news_documents_Item
	 */	
	public function getTranslation($lang)
	{
		return m_news_documents_Item::getInstanceByUniqueId($this->getUniqueId(), $lang);
	}

}